package com.dd.cloud.user.service.classroom.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dd.cloud.common.constants.ClassroomConstants;
import com.dd.cloud.user.entity.classroom.MediaContent;
import com.dd.cloud.user.entity.classroom.MediaPlaySite;
import com.dd.cloud.common.interceptor.UserInterceptor;
import com.dd.cloud.user.mapper.classroom.MediaPlaySiteMapper;
import com.dd.cloud.user.service.classroom.IMediaContentService;
import com.dd.cloud.user.service.classroom.IMediaPlaySiteService;
import com.dd.cloud.common.vo.LoginInfo;
import com.dd.cloud.common.enums.ZkExceptionEnum;
import com.dd.cloud.common.exception.ZekeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 媒体播放位置表 服务实现类
 * </p>
 *
 * @author czg
 * @since 2022-10-10
 */
@Service
public class MediaPlaySiteServiceImpl extends ServiceImpl<MediaPlaySiteMapper, MediaPlaySite> implements IMediaPlaySiteService {
    @Autowired
    IMediaContentService mediaContentService;

    @Override
    public void setMediaContentPlaySite(Integer id, Integer playSite, Long duration) {
        LoginInfo loginInfo = UserInterceptor.getLoginUserInfo(LoginInfo.UserType.DOCTOR).get();
        MediaContent content = mediaContentService.getById(id);
        if (content == null) {
            return;
        }
        UpdateWrapper<MediaPlaySite> uw = new UpdateWrapper<>();
        uw.lambda().eq(MediaPlaySite::getSectionId, content.getSectionId())
                .eq(MediaPlaySite::getUserId, loginInfo.getId())
                .eq(MediaPlaySite::getUserType, loginInfo.getType())
                .set(MediaPlaySite::getLast, ClassroomConstants.LastPlay.NO);
        //.set(MediaContent::getLast, ClassroomConstants.LastPlay.NO);
        boolean i = update(uw);
//        if (!i) {
//            throw new ZekeException(ZkExceptionEnum.UPDATE_ERROR);
//        }

        QueryWrapper<MediaPlaySite> qw = new QueryWrapper<>();
        qw.lambda().eq(MediaPlaySite::getContentId, id)
                .eq(MediaPlaySite::getUserId, loginInfo.getId())
                .eq(MediaPlaySite::getUserType, loginInfo.getType())
                .eq(MediaPlaySite::getStatus, ClassroomConstants.PlaySiteStatus.NORMAL);
        MediaPlaySite site = getOne(qw);
        if (site == null){
            site = new MediaPlaySite();
            site.setContentId(id);
            site.setCreateDate(LocalDateTime.now());
            site.setStatus(ClassroomConstants.PlaySiteStatus.NORMAL);
            site.setUserId(loginInfo.getId());
            site.setUserType(loginInfo.getType());
            site.setSectionId(content.getSectionId());
        }
        site.setLast(ClassroomConstants.LastPlay.YES);
        site.setPlaySite(playSite);
        i = saveOrUpdate(site);
        if (!i) {
            throw new ZekeException(ZkExceptionEnum.UPDATE_ERROR);
        }
    }
}
